<?php
namespace common\forms\login;

use Yii;
use yii\base\ModelEvent;

/**
 * 微信小程序登录
 */
class WxaLoginForm extends BaseLoginForm
{
    public $js_code;
    public $appid;
    public $access_token;
    public $grant_type = 'authorization_code';

    private $_user;

    const EVENT_BEFORE_LOGIN = 'beforeLogin';
    const EVENT_AFTER_LOGIN = 'afterLogin';

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username'=>'用户名',
            'mobile'=>'手机号码',
            'smscode'=>'验证码',
            'password'=>'密码'
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'string', 'min' => 2],

            ['mobile', 'trim'],
            ['mobile', 'required'],
            ['mobile', 'string', 'min' => 11, 'max' => 11],

            ['smscode', 'required'],
            ['smscode', 'validateSmsCode'],
            ['smscode', 'string', 'min' => 4, 'max' => 6],

            ['password', 'required']
        ];
    }

    public function scenarios(){
        $scenarios = parent::scenarios();
        $scenarios['username'] = ['username', 'password'];
        $scenarios['password'] = ['mobile', 'password'];
        $scenarios['smscode'] = ['mobile', 'smscode'];
        return $scenarios;
    }

    /**
     * 
     */
    public function beforeLogin()
    {
        $event = new ModelEvent;
        $this->trigger(self::EVENT_BEFORE_LOGIN, $event);

        return $event->isValid;
    }

    /**
     * 
     */
    public function afterLogin()
    {
        $this->trigger(self::EVENT_AFTER_LOGIN);
    }

    /**
     * 短信验证
     */
    public function validateSmsCode($attribute, $params)
    {
        //TODO 短信验证
    }

    /**
     * 会员登陆.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function login()
    {
        
        //https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
        if(!$user){
            return null;
        }

        return $this->vaildLogin($user);
    }

    

    

    public function getUser(){
        $userClassName = Yii::$app->getUser()->identityClass;
        if(!$this->_user){
            $this->_user = $userClassName::findByUsername($this->scenario == 'username' ? $this->username : $this->mobile);
        }        
        return $this->_user;
    }

    /**
     * 商家会员登陆通过ID.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function loginIdentity($id)
    {
        $userClassName = Yii::$app->getUser()->identityClass;
        $this->_user = $userClassName::findIdentity($id);
        if (!$this->beforeLogin()) {
            return false;
        }
        if(!$this->_user)return null;
        return $this->vaildLogin($this->_user);
    }

    private function vaildLogin($user){
        Yii::$app->user->login($user);
        $this->afterLogin();
        return $user;
    }
}
